from rms import rmsservice as db
from flask import Blueprint, jsonify, redirect, url_for, request
import logging, requests
import sys, traceback, os, re
from utils.tokenutil import TokenUtil

logger = logging.getLogger('rmsview')

rms = Blueprint("rms", __name__, url_prefix="/api/rms")

# 查询附近职位列表
@rms.route('/getPositionList', methods=['GET'])
def rmsgetPositionList():
    param = {}
    param['latitude'] = request.args.get('latitude')
    param['longitude'] = request.args.get('longitude')
    param['pcitycode'] = request.args.get('pcitycode')
    if request.args.get('keyword') is not None:
        if request.args.get('keyword') != '':
            param['keyword'] = request.args.get('keyword')
    if request.args.get('postId') is not None:
        if request.args.get('postId') != '':
            param['postId'] = request.args.get('postId')
    if request.args.get('industryId') is not None:
        if request.args.get('industryId') != '':
            param['industryId'] = request.args.get('industryId')    
    if request.args.get('salary') is not None:
        if request.args.get('salary') != '':
            param['salary'] = request.args.get('salary')          
    if request.args.get('nature') is not None:
        if request.args.get('nature') != '':
            param['nature'] = request.args.get('nature')           
    return jsonify(db.rmsgetPositionList(param))

# 查询职位详细信息
@rms.route('/getPositionDetail', methods=['GET'])
def rmsgetPositionDetail():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['id'] = request.args.get('id')
    param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.rmsgetPositionDetail(param))

# 查询公司职位列表
@rms.route('/getCompanyPositionList', methods=['GET'])
def rmsgetCompanyPositionList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    if request.args.get('id') is not None:
        param['id'] = request.args.get('id')
    if request.args.get('companyId') is not None:
        param['companyId'] = request.args.get('companyId')
    else:
        companyId = db.getCompanyId(TokenUtil.getMemberId(token))
        param['companyId'] = companyId
    if request.args.get('status') is not None:    
        param['status'] = request.args.get('status')
    if request.args.get('positionType') is not None:    
        param['positionType'] = request.args.get('positionType')    
    return jsonify(db.rmsgetCompanyPositionList(param))

# 查询公司信息
@rms.route('/getCompanyDetail', methods=['GET'])
def getCompanyDetail():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['id'] = request.args.get('id')
    param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.getCompanyDetail(param))

# 查询公司列表(字段精简)
@rms.route('/getCompanyListForSelect', methods=['GET'])
def getCompanyListForSelect():
    param = {}
    param['keyword'] = request.args.get('keyword')
    return jsonify(db.getCompanyListForSelect(param))

# 新增/更新公司信息
@rms.route('/updateCompany', methods=['POST'])
def updateCompany():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    if 'id' in formdata.keys():
        param['id'] = formdata['id']
    param['fullName'] = formdata['fullName']
    param['industryId'] = formdata['industryId']
    param['industryName'] = formdata['industryName']
    param['logo'] = formdata['logo']
    param['name'] = formdata['name']
    param['nature'] = formdata['nature']
    param['postName'] = formdata['postName']
    param['staffSize'] = formdata['staffSize']
    return jsonify(db.updateCompany(param))

# 公司认证
@rms.route('/updateAuth', methods=['POST'])
def updateAuth():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    if 'id' in formdata.keys():
        param['id'] = formdata['id']
    param['realName'] = formdata['realName']
    param['legalPerson'] = formdata['legalPerson']
    param['identity'] = formdata['identity']
    param['businessLicense'] = formdata['businessLicense']
    param['verifyStatus'] = formdata['verifyStatus']
    return jsonify(db.updateAuth(param))

# 新增/更新公司信息（详细）
@rms.route('/updateCompanyDetails', methods=['POST'])
def updateCompanyDetails():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    if 'id' in formdata.keys():
        param['id'] = formdata['id']
    param['fullName'] = formdata['fullName']
    param['industryId'] = formdata['industryId']
    param['industryName'] = formdata['industryName']
    param['logo'] = formdata['logo']
    param['name'] = formdata['name']
    param['nature'] = formdata['nature']
    param['staffSize'] = formdata['staffSize']
    param['restTime'] = formdata['restTime']
    param['workOvertime'] = formdata['workOvertime']
    param['welfare'] = formdata['welfare']
    param['startTime'] = formdata['startTime']
    param['endTime'] = formdata['endTime']
    param['addressName'] = formdata['addressName']
    param['address'] = formdata['address']
    param['addressHouse'] = formdata['addressHouse']
    param['latitude'] = formdata['latitude']
    param['longitude'] = formdata['longitude']
    param['city'] = formdata['city']
    param['citycode'] = formdata['citycode']
    param['pcity'] = formdata['pcity']
    param['pcitycode'] = formdata['pcitycode']
    param['companyProfile'] = formdata['companyProfile']
    param['album'] = formdata['album'] 
    return jsonify(db.updateCompanyDetails(param))

# 新增/更新职位信息
@rms.route('/updatePosition', methods=['POST'])
def updatePosition():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    if 'id' in formdata.keys():
        param['id'] = formdata['id']
    else:
        companyId = db.getCompanyId(TokenUtil.getMemberId(token))
        param['companyId'] = companyId
    param['positionType'] = formdata['positionType']
    param['ifParttime'] = formdata['ifParttime']
    param['postId'] = formdata['postId']
    param['postName'] = formdata['postName']
    param['minEducation'] = formdata['minEducation']
    param['expRequire'] = formdata['expRequire']
    param['phone'] = formdata['phone']
    param['salary'] = formdata['salary']
    param['descr'] = formdata['descr']
    param['addressName'] = formdata['addressName']
    param['address'] = formdata['address']
    param['addressHouse'] = formdata['addressHouse']
    param['latitude'] = formdata['latitude']
    param['longitude'] = formdata['longitude']
    param['startDate'] = formdata['startDate']
    param['endDate'] = formdata['endDate']
    param['shareNumber'] = formdata['shareNumber']
    param['skill'] = formdata['skill']
    return jsonify(db.updatePosition(param))

# 更新职位状态
@rms.route('/updatePositionStatus', methods=['POST'])
def updatePositionStatus():
    formdata = request.json
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['status'] = formdata['status']
    param['id'] = formdata['id']
    return jsonify(db.updatePositionStatus(param))

# 删除公司职位
@rms.route('/deletePositionById', methods=['GET'])
def deletePositionById():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deletePositionById(param))
